package com.health.article.sever.service.impl;

import com.health.article.common.domain.dto.ShmArticleDTO;
import com.health.article.common.domain.vo.ShmArticleVO;
import com.health.article.sever.mapper.ArticleMapper;
import com.health.article.sever.service.ArticleService;
import com.health.common.security.utils.SecurityUtils;
import com.health.system.common.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 查询所有文章
     * @return
     */
    @Override
    public List<ShmArticleVO> selectArticleAll() {
        List<ShmArticleVO> shmArticleVOS = articleMapper.selectArticleAll();
        return shmArticleVOS;
    }

    /**
     * 医生/患者发布创作/保存草稿
     * @param shmArticleDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertArticle(ShmArticleDTO shmArticleDTO) {

        articleMapper.insertArticle(shmArticleDTO);

        articleMapper.insertArticleSubject(shmArticleDTO.getId(),shmArticleDTO.getSubjectIds());

    }

    @Override
    public List<ShmArticleVO> selectMyDraftAndArticle(Long articleStatus) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userid = loginUser.getUserid();

        List<ShmArticleVO> shmArticleVOS = articleMapper.selectMyDraftAndArticle(userid, articleStatus);

        if (shmArticleVOS.size() == 0){
            throw new RuntimeException("暂无草稿/创作");
        }

        return shmArticleVOS;
    }
}
